package cn.line5.module;

import java.util.List;
import java.util.Map;

import cn.line5.seam.annotations.Name;
import cn.line5.seam.db.PageResult;
import cn.line5.seam.db.QueryFactory;

@Name("moduleDao")
/**
 * <h1>模块管理数据库实现类</h1>
 * <br/>
 * 
 * @author Liu,Xiaojun
 */
public class ModuleDaoImpl implements ModuleDao {

	private final String moduleSql = "SELECT MODULE_ID as moduleId, MODULE_NAME as moduleName, MODULE_DESC as moduleDesc, MODULE_TYPE as moduleType, MODULE_URL as moduleUrl, MODULE_SORT as moduleSort  FROM T_MODULE";
	private final String functionSql = "SELECT FUNCTION_ID as functionId, FUNCTION_NAME as functionName, FUNCTION_DESC as functionDesc, FUNCTION_SORT as functionSort  FROM T_MODULE_FUNCTION";
	private final String pointSql = "SELECT POINT_ID as pointId, FUNCTION_ID as functionId, POINT_NAME as pointName, COMPONENT_NAME as componentName, METHOD_NAME as methodName, POINT_SORT as pointSort  FROM T_MODULE_FUNCTION_POINT";

	/**
	 * 查询子模块列表
	 */
	public List<ModuleVo> getModuleList(String parentId) {
		return QueryFactory.createQuery(
				moduleSql + " WHERE MODULE_ID LIKE ? ORDER BY MODULE_SORT")
				.setParameter(1, parentId + "___").list(ModuleVo.class);
	}

	/**
	 * 查询子模块分页列表
	 */
	public PageResult<ModuleVo> getModulePageList(String parentId,
			int currentPage) {
		return QueryFactory.createQuery(
				moduleSql + " WHERE MODULE_ID LIKE ? ORDER BY MODULE_SORT")
				.setParameter(1, parentId + "___").setCurrentPage(
						currentPage > 0 ? currentPage : 1).setPageRows(
						ModuleConstants.PAGE_ROWS).pageList(ModuleVo.class);
	}

	/**
	 * 查询模块信息
	 */
	public ModuleVo getModule(String moduleId) {
		return QueryFactory.createQuery(moduleSql + " WHERE MODULE_ID=?")
				.setParameter(1, moduleId).single(ModuleVo.class);
	}

	/**
	 * 获取同级模块最大ID
	 */
	public String getModuleMaxId(String parentId) {
		Map<String, Object> m = QueryFactory
				.createQuery(
						"SELECT MAX(MODULE_ID) as maxid FROM T_MODULE WHERE MODULE_ID LIKE ?")
				.setParameter(1, parentId + "___").single();
		if (m != null && m.containsKey("maxid") && m.get("maxid") != null)
			return (String) m.get("maxid");
		else
			return parentId + "000";
	}

	/**
	 * 获取同级模块最大排序
	 */
	public Integer getModuleMaxSort(String parentId) {
		Map<String, Object> m = QueryFactory
				.createQuery(
						"SELECT MAX(MODULE_SORT) as maxsort FROM T_MODULE WHERE MODULE_ID LIKE ?")
				.setParameter(1, parentId + "___").single();
		if (m != null && m.containsKey("maxsort") && m.get("maxsort") != null)
			return (Integer) m.get("maxsort");
		else
			return 0;
	}

	/**
	 * 添加模块
	 */
	public Boolean addModule(ModuleVo vo) {
		return QueryFactory
				.createExecute(
						"iNSERT INTO T_MODULE VALUES(:moduleId, :moduleName, :moduleDesc, :moduleType, :moduleUrl, :moduleSort)",
						vo).execute() > 0;
	}

	/**
	 * 修改模块
	 */
	public Boolean modifyModule(ModuleVo vo) {
		return QueryFactory
				.createExecute(
						"UPDATE T_MODULE SET MODULE_NAME=:moduleName, MODULE_DESC=:moduleDesc, MODULE_URL=:moduleUrl where MODULE_ID=:moduleId",
						vo).execute() > 0;
	}

	/**
	 * 保存模块排序
	 */
	public Boolean saveModuleSort(ModuleVo vo) {
		return QueryFactory
				.createExecute(
						"UPDATE T_MODULE SET MODULE_SORT=:moduleSort where MODULE_ID=:moduleId",
						vo).execute() > 0;
	}

	/**
	 * 删除模块及子模块
	 */
	public Boolean deleteModule(String moduleId) {
		return QueryFactory.createExecute(
				"DELETE FROM T_MODULE WHERE MODULE_ID LIKE ?").setParameter(1,
				moduleId + "%").execute() >= 0;
	}

	/**
	 * 删除模块下的功能
	 */
	public Boolean deleteFunctionByModule(String moduleId) {
		return QueryFactory.createExecute(
				"DELETE FROM T_MODULE_FUNCTION WHERE FUNCTION_ID LIKE ?")
				.setParameter(1, moduleId + "%").execute() >= 0;
	}

	/**
	 * 删除模块下的功能点
	 */
	public Boolean deletePointByModule(String moduleId) {
		return QueryFactory.createExecute(
				"DELETE FROM T_MODULE_FUNCTION_POINT WHERE FUNCTION_ID LIKE ?")
				.setParameter(1, moduleId + "%").execute() >= 0;
	}

	/**
	 * 获取模块下功能列表
	 */
	public List<ModuleFunctionVo> getFunctionList(String moduleId) {
		return QueryFactory.createQuery(
				functionSql
						+ " WHERE FUNCTION_ID LIKE ? ORDER BY FUNCTION_SORT")
				.setParameter(1, moduleId + "___").list(ModuleFunctionVo.class);
	}

	/**
	 * 获取模块下功能分页列表
	 */
	public PageResult<ModuleFunctionVo> getFunctionPageList(String moduleId,
			int currentPage) {
		return QueryFactory.createQuery(
				functionSql
						+ " WHERE FUNCTION_ID LIKE ? ORDER BY FUNCTION_SORT")
				.setParameter(1, moduleId + "___").setCurrentPage(
						currentPage > 0 ? currentPage : 1).setPageRows(
						ModuleConstants.PAGE_ROWS).pageList(
						ModuleFunctionVo.class);
	}

	/**
	 * 获取功能信息
	 */
	public ModuleFunctionVo getFunction(String functionId) {
		return QueryFactory.createQuery(functionSql + " WHERE FUNCTION_ID=?")
				.setParameter(1, functionId).single(ModuleFunctionVo.class);
	}

	/**
	 * 获取同模块下功能最大ID
	 */
	public String getFunctionMaxId(String moduleId) {
		Map<String, Object> m = QueryFactory
				.createQuery(
						"SELECT MAX(FUNCTION_ID) as maxid FROM T_MODULE_FUNCTION WHERE FUNCTION_ID LIKE ?")
				.setParameter(1, moduleId + "___").single();
		if (m != null && m.containsKey("maxid") && m.get("maxid") != null)
			return (String) m.get("maxid");
		else
			return moduleId + "000";
	}

	/**
	 * 获取同模块下功能最大排序
	 */
	public Integer getFunctionMaxSort(String moduleId) {
		Map<String, Object> m = QueryFactory
				.createQuery(
						"SELECT MAX(FUNCTION_SORT) as maxsort FROM T_MODULE_FUNCTION WHERE FUNCTION_ID LIKE ?")
				.setParameter(1, moduleId + "___").single();
		if (m != null && m.containsKey("maxsort") && m.get("maxsort") != null)
			return (Integer) m.get("maxsort");
		else
			return 0;
	}

	/**
	 * 添加功能
	 */
	public Boolean addFunction(ModuleFunctionVo vo) {
		return QueryFactory
				.createExecute(
						"iNSERT INTO T_MODULE_FUNCTION VALUES(:functionId, :functionName, :functionDesc, :functionSort)",
						vo).execute() > 0;
	}

	/**
	 * 修改功能
	 */
	public Boolean modifyFunction(ModuleFunctionVo vo) {
		return QueryFactory
				.createExecute(
						"UPDATE T_MODULE_FUNCTION SET FUNCTION_NAME=:functionName, FUNCTION_DESC=:functionDesc where FUNCTION_ID=:functionId",
						vo).execute() > 0;
	}

	/**
	 * 保存功能排序
	 */
	public Boolean saveFunctionSort(ModuleFunctionVo vo) {
		return QueryFactory
				.createExecute(
						"UPDATE T_MODULE_FUNCTION SET FUNCTION_SORT=:functionSort where FUNCTION_ID=:functionId",
						vo).execute() > 0;
	}

	/**
	 * 删除功能
	 */
	public Boolean deleteFunction(String functionId) {
		return QueryFactory.createExecute(
				"DELETE FROM T_MODULE_FUNCTION WHERE FUNCTION_ID = ?")
				.setParameter(1, functionId).execute() >= 0;
	}

	/**
	 * 获取功能下功能点列表
	 */
	public List<ModuleFunctionPointVo> getPointList(String functionId) {
		return QueryFactory.createQuery(
				pointSql + " WHERE FUNCTION_ID = ? ORDER BY POINT_SORT")
				.setParameter(1, functionId).list(ModuleFunctionPointVo.class);
	}

	/**
	 * 获取所有功能点列表
	 */
	public List<ModuleFunctionPointVo> getPointList() {
		return QueryFactory.createQuery(pointSql + " ORDER BY POINT_SORT")
				.list(ModuleFunctionPointVo.class);
	}

	/**
	 * 获取功能下功能点分页列表
	 */
	public PageResult<ModuleFunctionPointVo> getPointPageList(
			String functionId, int currentPage) {
		return QueryFactory.createQuery(
				pointSql + " WHERE FUNCTION_ID = ? ORDER BY POINT_SORT")
				.setParameter(1, functionId).setCurrentPage(
						currentPage > 0 ? currentPage : 1).setPageRows(
						ModuleConstants.PAGE_ROWS).pageList(
						ModuleFunctionPointVo.class);
	}

	/**
	 * 获取功能点信息
	 */
	public ModuleFunctionPointVo getPoint(Integer pointId) {
		return QueryFactory.createQuery(pointSql + " WHERE POINT_ID=?")
				.setParameter(1, pointId).single(ModuleFunctionPointVo.class);
	}

	/**
	 * 获取同功能下功能最大排序
	 */
	public Integer getPointMaxSort(String functionId) {
		Map<String, Object> m = QueryFactory
				.createQuery(
						"SELECT MAX(POINT_SORT) as maxsort FROM T_MODULE_FUNCTION_POINT WHERE FUNCTION_ID = ?")
				.setParameter(1, functionId).single();
		if (m != null && m.containsKey("maxsort") && m.get("maxsort") != null)
			return (Integer) m.get("maxsort");
		else
			return 0;
	}

	/**
	 * 添加功能点
	 */
	public Boolean addPoint(ModuleFunctionPointVo vo) {
		return QueryFactory
				.createExecute(
						"iNSERT INTO T_MODULE_FUNCTION_POINT(FUNCTION_ID, POINT_NAME, COMPONENT_NAME, METHOD_NAME, POINT_SORT) VALUES(:functionId, :pointName, :componentName, :methodName, :pointSort)",
						vo).execute() > 0;
	}

	/**
	 * 修改功能点
	 */
	public Boolean modifyPoint(ModuleFunctionPointVo vo) {
		return QueryFactory
				.createExecute(
						"UPDATE T_MODULE_FUNCTION_POINT SET POINT_NAME=:pointName, COMPONENT_NAME=:componentName, METHOD_NAME=:methodName where POINT_ID=:pointId",
						vo).execute() > 0;
	}

	/**
	 * 保存功能点排序
	 */
	public Boolean savePointSort(ModuleFunctionPointVo vo) {
		return QueryFactory
				.createExecute(
						"UPDATE T_MODULE_FUNCTION_POINT SET POINT_SORT=:pointSort where POINT_ID=:pointId",
						vo).execute() > 0;
	}

	/**
	 * 删除功能点
	 */
	public Boolean deletePoint(Integer pointId) {
		return QueryFactory.createExecute(
				"DELETE FROM T_MODULE_FUNCTION_POINT WHERE POINT_ID = ?")
				.setParameter(1, pointId).execute() >= 0;
	}
}
